/*
 * To change this license header, choose License Headers in Project Properties.
 * To change ths template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tienda.zapateria.siszap.gui.control;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import javax.swing.FocusManager;
import javax.swing.JOptionPane;
import org.rx.cr.gui.JInternalFrameUC;
import org.rx.cr.util.Utilitarios;
import static org.rx.cr.util.Utilitarios.alinearDatosColumnaTablaCentro;
import static org.rx.cr.util.Utilitarios.disableComponent;
import static org.rx.cr.util.Utilitarios.enableComponent;
import static org.rx.cr.util.Utilitarios.formateaAnchoColumnaTabla;
import static org.rx.cr.util.Utilitarios.resetComponents;
import tienda.zapateria.siszap.be.BETipo;
import tienda.zapateria.siszap.bl.BLTipo;
import tienda.zapateria.siszap.gui.JF_Principal;
import tienda.zapateria.siszap.gui.control.model.ModelTipo;

/**
 *
 * @author max
 */
public class JIF_AdministrarTipo extends JInternalFrameUC {

    JF_Principal parent;
    BETipo be;
    BLTipo bl;
    ModelTipo model;

    public JIF_AdministrarTipo(JF_Principal parent) {
        initComponents();
        this.parent = parent;
        bl = new BLTipo(parent.getConf());
        model = new ModelTipo();
        jtTipo.setModel(model);
        personalizaVistaTabla();
        _init();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbAgregar = new javax.swing.JButton();
        jbEditar = new javax.swing.JButton();
        jbGuardar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txfNombre = new javax.swing.JTextField()
        {
            @Override
            protected void paintComponent(java.awt.Graphics g) {
                super.paintComponent(g);
                if(isEnabled() && getText().isEmpty() && ! (FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)){
                    Graphics2D g2 = (Graphics2D)g.create();
                    g2.setColor(Color.gray);
                    g2.setFont(getFont().deriveFont(Font.ITALIC));
                    g2.drawString("Ingrese Nombres",5,18);
                    g2.dispose();
                }
            }
        }
        ;
        jPanel1 = new javax.swing.JPanel();
        txfBuscar = new javax.swing.JTextField()
        {
            @Override
            protected void paintComponent(java.awt.Graphics g) {
                super.paintComponent(g);
                if(isEnabled() && getText().isEmpty() && ! (FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)){
                    Graphics2D g2 = (Graphics2D)g.create();
                    g2.setColor(Color.gray);
                    g2.setFont(getFont().deriveFont(Font.ITALIC));
                    g2.drawString("Ingrese el tipo de zapato",5,18);
                    g2.dispose();
                }
            }
        }
        ;
        jbBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtTipo = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);

        jbAgregar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/Nuevo.png"))); // NOI18N
        jbAgregar.setText("NUEVO");
        jbAgregar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbAgregar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbAgregar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbEditar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/Modificar_Min.png"))); // NOI18N
        jbEditar.setText("EDITAR");
        jbEditar.setEnabled(false);
        jbEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEditar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbGuardar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/Guardar_Min.png"))); // NOI18N
        jbGuardar.setText("GUARDAR");
        jbGuardar.setEnabled(false);
        jbGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbGuardar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbCancelar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/Cancelar_Min.png"))); // NOI18N
        jbCancelar.setText("CANCEL");
        jbCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbCancelar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tipo de zapato", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        txfNombre.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txfNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(txfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BUSCAR  TIPO", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tienda/zapateria/siszap/imagen/Buscar.png"))); // NOI18N

        jtTipo.setAutoCreateRowSorter(true);
        jtTipo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtTipo);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txfBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbBuscar)
                    .addComponent(txfBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbCancelar)))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbAgregar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbEditar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbGuardar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbGuardar;
    private javax.swing.JTable jtTipo;
    private javax.swing.JTextField txfBuscar;
    private javax.swing.JTextField txfNombre;
    // End of variables declaration//GEN-END:variables

    @Override
    public void _cargarCombos() throws Exception {
    }

    @Override
    public void _agregaValidadoresControl() {
        Utilitarios.addEnterDoclickEvent(new Object[][]{{txfBuscar, jbBuscar}});
        _addOperationEventsButtons(jbAgregar, jbEditar, jbGuardar, jbCancelar);
        _addRestoreDataEventsTable(jtTipo);
        _addSearchEventsButton(jbBuscar);
        Utilitarios.addCharacterValidatorEvent(new Object[][]{ //{txfEstablecimientoSalud, ALFABETICO_CM, 160},
        //         {txfNumMaquinas, NUMERICO, 20},
        //        {txfNumTrabajadores, NUMERICO, 20},
        //                    {txfTelefono, NUMERICO, 15}
        });
    }

    @Override
    public boolean _isDatosValidosBE() {
        return Utilitarios.isDatosLlenos(new Object[][]{
            {txfNombre, "Ingresar la denominación del tipo."}
// {txfEstablecimientoSalud, "Ingresar el nombre del establecimiento"},
        // {txfCodigoEstablecimiento, "Ingresar el código del establecimiento"},
        // {txfDireccion, "Ingresar la dirección del establecimiento"},
        });
    }

    @Override
    public int _registrarBE() throws Exception {
        try {
            be = new BETipo();
            be.setNombre(txfNombre.getText());
            bl.registrar(be);
            JOptionPane.showMessageDialog(this, "Registro exitoso. ", "Administrar Tipo de Zapato", JOptionPane.INFORMATION_MESSAGE);

            return 1;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error en el registro: " + e, "Atención", JOptionPane.ERROR_MESSAGE);
            return 0;
        }

    }

    @Override
    public int _guardarCambiosBE() throws Exception {
        try {
            be = model.get(jtTipo.getSelectedRow());
            be.setNombre(txfNombre.getText());
            bl.actualizarRegistro(be);
            JOptionPane.showMessageDialog(this, "Actualización exitosa. ", "Administrar Tipo de Zapato", JOptionPane.INFORMATION_MESSAGE);

            return 1;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error en la actualización: " + e, "Atención", JOptionPane.ERROR_MESSAGE);
            return 0;
        }
    }

    @Override
    public void _listarBEs() throws Exception {
        model.addAll(bl.buscarReferencia(txfBuscar.getText()));
        personalizaVistaTabla();
    }

    @Override
    public void _recuperarDatosBE() throws Exception {
        if (jtTipo.getSelectedRow() >= 0) {
            be = model.get(jtTipo.getSelectedRow());
            txfNombre.setText(be.getNombre());
            jbEditar.setEnabled(true);
        }
    }

    @Override
    public void _deshabilitarControlesDatos() {
        disableComponent(new Object[]{
            txfNombre
        });
    }

    @Override
    public void _habilitarControlesDatos() {
        enableComponent(new Object[]{
            txfNombre
        });
    }

    @Override
    public void _resetControls() {
        resetComponents(new Object[]{
            txfNombre
        });
    }

    private void personalizaVistaTabla() {
        alinearDatosColumnaTablaCentro(0, jtTipo);
        formateaAnchoColumnaTabla(0, jtTipo, 249);

    }
}
